#include "registwidget.h"
#include "ui_registwidget.h"
#include "loginwidget.h"
#include "../MyTcp2/tcpsession.h"

RegistWidget::RegistWidget(TcpSession *session, LoginWidget *loginWidget, QWidget *parent) :
    QWidget(parent), ui(new Ui::RegistWidget),loginWidget(loginWidget),session(session)
{
    //界面
    ui->setupUi(this);
    this->setWindowTitle("注册");
    ui->lineEdit_username->clear();
    ui->lineEdit_pwd->clear();
    ui->lineEdit_nickname->clear();

    //tcp的回复事件
    QObject::connect(session, &TcpSession::reg_ret_signal, this, &RegistWidget::on_reg_ret_signal);
}

void RegistWidget::on_pushButton_reg_clicked()
{
    //校验输入的内容是否OK
    QString username = ui->lineEdit_username->text();
    if(username.isEmpty() || username.length() == 0){
        QMessageBox::critical(this, "用户名输入错误", "用户名不能为空");
        return;
    }

    QString passwd=ui->lineEdit_pwd->text();
    if(passwd.isEmpty() || passwd.length() == 0){
        QMessageBox::critical(this, "密码输入错误", "密码不能为空");
        return;
    }

    QString nickname=ui->lineEdit_nickname->text();
    if(nickname.isEmpty() || nickname.length() == 0){
        QMessageBox::critical(this, "昵称输入错误", "昵称不能为空");
        return;
    }


    //发送信息
    Pack pack;
    pack.setType(REGIST);
    pack.setFrame(CALL);
    pack.append(username);
    pack.append(passwd);
    pack.append(nickname);
    //
    session->send((char*)&pack, pack.getPacksize());
}

void RegistWidget::on_pushButton_cancel_clicked()
{
    this->hide();
    loginWidget->show();
}

void RegistWidget::on_reg_ret_signal(bool res, const QString &msg)
{
    if(res==true){
        QMessageBox::information(this, "注册成功", "注册成功!");
        //界面跳转
        on_pushButton_cancel_clicked();
    }else{
        QMessageBox::critical(this, "注册失败", "注册失败: "+msg);
    }
}


